﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using System.Text.RegularExpressions;
using Code.BLL.Arithmetic.Comm;

namespace Code.BLL.Arithmetic
{
    /// <summary>
    /// 好彩1
    /// </summary>
    internal class HC1 : LotteryTypeProduct
    {
        public override PlayTypeProduct GetPlayType(Code.Models.PlayType_E playType)
        {
            PlayTypeProduct product;
            switch (playType)
            {
                case PlayType_E.Number:
                    product= new Number(regNumber);
                    break;
                case PlayType_E.Animals:
                    product= new Animals(regAnimals);
                    break;
                case PlayType_E.Season:
                    product= new Season(regSeason);
                    break;
                case PlayType_E.Position:
                    product= new Position(regPosition);
                    break;
                default:
                    throw new Exception("好彩彩种无当前玩法:"+playType);
            }
            product.lotteryType = LotteryType.HaoCai1;
            return product;
        }

        private static Regex regNumber = new Regex(@"^(?![0-9a-z]{0,}(?<num>[0-9a-z])[0-9a-z]{0,}\k<num>)[0-9a-z]{1,}$", RegexOptions.Compiled);

        private static Regex regAnimals = new Regex(@"^(?![鼠牛虎兔龙蛇马羊猴鸡狗猪]{0,}(?<num>[鼠牛虎兔龙蛇马羊猴鸡狗猪])[鼠牛虎兔龙蛇马羊猴鸡狗猪]{0,}\k<num>)[鼠牛虎兔龙蛇马羊猴鸡狗猪]{1,}$", RegexOptions.Compiled);

        private static Regex regSeason = new Regex(@"^(?![春夏秋冬]{0,}(?<num>[春夏秋冬])[春夏秋冬]{0,}\k<num>)[春夏秋冬]{1,10}$", RegexOptions.Compiled);

        private static Regex regPosition = new Regex(@"^(?![东南西北]{0,}(?<num>[东南西北])[东南西北]{0,}\k<num>)[东南西北]{1,10}$", RegexOptions.Compiled);
    }
    public abstract class HC1PlayTypeBase:PlayTypeProduct
    { 
        private Regex _UserChoseDoubleNumberFormat;
        private string _OpenCode ;
        private PlayType_E _Type;
        public  HC1PlayTypeBase(Regex regDouble,PlayType_E type)
            : base(regDouble)
        {
            this._UserChoseDoubleNumberFormat = regDouble;
            _Type = type;
        }
        /// <summary>
        /// 根据用户选择的号码所在的组名，返回所对应的号码数组
        /// </summary>
        /// <param name="name">用户选择的号码所在的组名</param>
        /// <returns></returns>
        public abstract int[] GetNums(string name);

        protected override IEnumerable<string> _GetWinNums(string userChoseNumber, string openCode)
        {
            _OpenCode = _Type.GetOpenCode(openCode, base.lotteryType);
            if (userChoseNumber == null)
                throw new ArgumentNullException("userChoseNumber","参数不能为空");
            userChoseNumber = userChoseNumber.Replace(",", "");
            if (!_UserChoseDoubleNumberFormat.IsMatch(userChoseNumber))
                throw new ArgumentException("参数数格式不正确", "userChoseNumber");

            int openCode1;
            if (!int.TryParse(_OpenCode,out openCode1))
                throw new ArgumentException("参数数格式不正确"+_OpenCode, "openCode");
            List<string> winNums = new List<string>();
            foreach (var item in userChoseNumber)
            {

                int[] nums = GetNums(item.ToString());
                if (nums.Length != 0)
                {
                    if (nums.Contains(openCode1))
                        winNums.Add(item.ToString());
                }

            }
            return winNums;
        }


        protected override int _GetNumCount(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                throw new ArgumentNullException();
            return userChoseNumber.Split(',').Length;
        }

        protected override List<string> _GetChoseNumberList(string userChoseNumber)
        {
            if (userChoseNumber == null)
                throw new ArgumentNullException("userChoseNumber", "参数不能为空");
            base.ChoseNumberList = userChoseNumber.Split(',').ToList();
            return base.ChoseNumberList;
        }
        protected override string _RegistNum(string userChoseNumber)
        {
            if (userChoseNumber == null || userChoseNumber == "")
                return null;
            var number = userChoseNumber.Replace(",", "");
            Regex r = _UserChoseDoubleNumberFormat;

            if (r.IsMatch(number))
                return userChoseNumber;
            else
                return null;
        }
    }
    /// <summary>
    /// 数字 
    /// </summary>
    internal class Number : HC1PlayTypeBase
    {
        private string _OpenCode;
        Regex _UserChoseDoubleNumberFormat;
        /// <summary>
        /// 数字的构造函数
        /// </summary>
        /// <param name="regDouble">复式的号码格式</param>
        public Number(Regex regDouble)
            : base(regDouble, PlayType_E.Number)
        {
            _UserChoseDoubleNumberFormat = regDouble;
        }
        public override int[] GetNums(string name)
        {
            int number;
            if (int.TryParse(name, out number))
                return new int[]{number};
            return new int[]{-1};
        }
        protected override IEnumerable<string> _GetWinNums(string userChoseNumber, string openCode)
        {
            _OpenCode = PlayType_E.Number.GetOpenCode(openCode, base.lotteryType);
            if (userChoseNumber == null)
                throw new ArgumentNullException("userChoseNumber", "参数不能为空");
            var newUserChoseNumber = string.Join("", userChoseNumber.Split(',').Select(p => 
            { 
                int i;
                if (!int.TryParse(p, out i))
                    throw new ArgumentException("参数数格式不正确", "userChoseNumber");
                if (i > 10)
                    return ((char)(int.Parse(p) + (97 - 11))).ToString();
                else if (i == 10)
                    return "0";
                return i.ToString();
            }).ToArray());
            if (!_UserChoseDoubleNumberFormat.IsMatch(newUserChoseNumber))
                throw new ArgumentException("参数数格式不正确", "userChoseNumber");

            int openCode1;
            if (!int.TryParse(_OpenCode,out openCode1))
                throw new ArgumentException("参数数格式不正确"+_OpenCode, "openCode");
            List<string> winNums = new List<string>();
            foreach (var item in userChoseNumber.Split(','))
            {
                if (openCode1 == int.Parse(item))
                    winNums.Add(item);
            }
            return winNums;
        }
        protected override string _RegistNum(string userChoseNumber)
        {
            if (userChoseNumber == null || userChoseNumber == "")
                return null;
            var number = string.Join("", userChoseNumber.Split(',').Select(p =>
                {
                    int i;
                    if (!int.TryParse(p, out i))
                        throw new ArgumentException("参数数格式不正确", "userChoseNumber");
                    if (i > 10)
                        return ((char)(int.Parse(p) + (97 - 11))).ToString();
                    else if (i == 10)
                        return "0";
                    return i.ToString();
                }).ToArray());

            Regex r = _UserChoseDoubleNumberFormat;
            if (r.IsMatch(number))
                return userChoseNumber;
            else
                return null;
        }
    }
    /// <summary>
    /// 生肖
    /// </summary>
    internal class Animals : HC1PlayTypeBase
    { 
        /// <summary>
        /// 生肖的构造函数
        /// </summary>
        /// <param name="regDouble">复式的号码格式</param>
        /// <param name="regSingle">单式的号码格式</param>
        /// <param name="openCode">开奖号码</param>
        public Animals(Regex regDouble)
            : base(regDouble,PlayType_E.Animals)
        { 
        }
        public override int[] GetNums(string name)
        {//鼠牛虎兔龙蛇马羊猴鸡狗猪
            switch (name)
            {
                case "鼠":
                    return new int[] { 1, 13, 25 };
                case "牛":
                    return new int[] { 2, 14, 26 };
                case "虎":
                    return new int[] { 3, 15, 27 };
                case "兔":
                    return new int[] { 4, 16, 28 };
                case "龙":
                    return new int[] { 5, 17, 29 };
                case "蛇":
                    return new int[] { 6, 18, 30 };
                case "马":
                    return new int[] { 7, 19, 31 };
                case "羊":
                    return new int[] { 8, 20, 32 };
                case "猴":
                    return new int[] { 9, 21, 33 };
                case "鸡":
                    return new int[] { 10, 22, 34 };
                case "狗":
                    return new int[] { 11, 23, 35 };
                case "猪":
                    return new int[] { 12, 24, 36 };
                default:
                    throw new Exception("没有当前玩法！");
            }

        }
    }
    /// <summary>
    /// 季节
    /// </summary>
    internal class Season : HC1PlayTypeBase
    {
        /// <summary>
        /// 季节的构造函数
        /// </summary>
        /// <param name="regDouble">复式的号码格式</param>
        /// <param name="regSingle">单式的号码格式</param>
        /// <param name="openCode">开奖号码</param>
        public Season(Regex regDouble)
            : base(regDouble,PlayType_E.Season)
        { 
        }
        public override int[] GetNums(string name)
        {//春夏秋冬
            switch (name)
            {
                case "春":
                    return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                case "夏":
                    return new int[] { 10, 11, 12, 13, 14, 15, 16, 17, 18 };
                case "秋":
                    return new int[] { 19, 20, 21, 22, 23, 24, 25, 26, 27 };
                case "冬":
                    return new int[] { 28, 29, 30, 31, 32, 33, 34, 35, 36 };
                default:
                    throw new Exception("没有当前玩法！");
            }
        }
    }
    /// <summary>
    /// 方位
    /// </summary>
    internal class Position : HC1PlayTypeBase
    {
        /// <summary>
        /// 方位的构造函数
        /// </summary>
        /// <param name="regDouble">复式的号码格式</param>
        /// <param name="regSingle">单式的号码格式</param>
        /// <param name="openCode">开奖号码</param>
        public Position(Regex regDouble )
            : base(regDouble,PlayType_E.Position)
        { 
        }
        public override int[] GetNums(string name)
        {//东南西北
            switch (name)
            {
                case "东":
                    return new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17 };
                case "南":
                    return new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18 };
                case "西":
                    return new int[] { 19, 21, 23, 25, 27, 29, 31, 33, 35 };
                case "北":
                    return new int[] { 20, 22, 24, 26, 28, 30, 32, 34, 36 };
                default:
                    throw new Exception("没有当前玩法！");
            }
        }
    }
}
